<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage core
 * @version $Id$
 */

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_loader.php');

/**	
 * Resource File Resolver Tests
 *	
 * @package auxo
 * @subpackage core	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_ResourceFileResolver_testcase extends PHPUnit_Framework_testcase  {
	public $resolver = NULL;
	public $loader = NULL;
    /**
	 * Sets up a test enviroment of this testcase.
	 */
	public function setUp() {
		$this->loader = new tx_auxo_loader();
		$this->resolver = new tx_auxo_ResourceFileResolver(t3lib_extMgm::extPath('auxo'));
	}
	
	/**
	 * Testcase: if Resolver is available
	 * 
	 * @return void
	 */
	public function testcaseAvailable() {
		$this->assertTrue($this->resolver instanceof tx_auxo_ResourceFileResolver);
	}

	/**
	 * Testcase: Absolute path detection windows style 
	 * 
	 * @return void
	 */
	public function testcaseIsAbsolutePathWindowsStyle() {
		$this->assertTrue($this->resolver->isAbsolutePath('C:\temp'));
		$this->assertFalse($this->resolver->isAbsolutePath('C:temp\to'));
	}

	/**
	 * Testcase: Absolute path detection unix style 
	 * 
	 * @return void
	 */
	public function testcaseIsAbsolutePathUnixStyle() {
		$this->assertTrue($this->resolver->isAbsolutePath('/temp'));
		$this->assertFalse($this->resolver->isAbsolutePath('temp/to'));		
	}	
	
	/**
	 * Testcase: Detects a file in a relative given sub directory of $home
	 * 
	 * @return void
	 */
	public function testcaseFileInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], $path . '/' . 'testA.xml');
	}

	/**
	 * Testcase: Detects a file with trailing wildcard in a relative 
	 * of a defined home directory
	 * 
	 * @return void
	 */
	public function testcaseFileWithWildcardSuffixInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . 'test*.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], $path . '/' . 'testA.xml');
	}

	/**
	 * Testcase: Detects a file with leading wildcard in a relative path 
	 * of a defined home directory
	 * 
	 * @return void
	 */
	public function testcaseFileWithWildcardPrefixInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . '*test.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], $path . '/' . 'Btest.xml');
	}

	/**
	 * Testcase: Detects a file with enclosed with wildcards in a 
	 * relative path of a defined home directory
	 * 
	 * @return void
	 */
	public function testcaseFileWithWildcardsEnclosedInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . '*test*.xml');
		$this->assertEquals(count($files), 2);
		$this->assertEquals($files[0], $path . '/' . 'Btest.xml');		
		$this->assertEquals($files[1], $path . '/' . 'testA.xml');
	}

	/**
	 * Testcase: Detects a file with '?' wildcards in a 
	 * relative path (home directory)
	 * 
	 * @return void
	 */
	public function testcaseFileWithQuestionMarkInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . '?est?.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], $path . '/' . 'testA.xml');
	}	

	/**
	 * Testcase: Detects a file with leading wildcard in a 
	 * relative path 
	 * 
	 * @return void
	 */
	public function testcaseFileWithWildcardPrefixInRelativePath() {
		$path = 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . '*test.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . $path . '/' . 'Btest.xml');
	}

	/**
	 * Testcase: Detects a file with a trailing wildcard in a 
	 * relative path 
	 * 
	 * @return void
	 */
	public function testcaseFileWithWildcardSuffixInRelativePath() {
		$path = 'tests/fixtures';
		$files = $this->resolver->resolve($path . '/' . 'test*.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . $path . '/' . 'testA.xml');
	}	
	
	/**
	 * Testcase: Detects a file in an absolute path that includes a
	 * wildcard at the end
	 * 
	 * @return void
	 */
	public function testcaseDirectoryWithWildcardSuffixInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'tests/fix*';
		$files = $this->resolver->resolve($path . '/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . 'tests/fixtures' . '/' . 'testA.xml');
	}	

	/**
	 * Testcase: Detects a file in an absolute path that includes 
	 * a wildcard in between.
	 * 
	 * @return void
	 */
	public function testcaseDirectoryInTreeWithWildcardSuffixInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . 'test*/fixtures';
		$files = $this->resolver->resolve($path . '/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . 'tests/fixtures' . '/' . 'testA.xml');
	}

	/**
	 * Testcase: Detects a file in an absolute path that includes 
	 * only a wildcard in between.
	 * 
	 * @return void
	 */
	public function testcaseDirectoryInTreeWithStarWildcardInAbsolutePath() {
		$path = t3lib_extMgm::extPath('auxo') . '*/fixtures';
		$files = $this->resolver->resolve($path . '/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . 'tests/fixtures' . '/' . 'testA.xml');
	}	

	/**
	 * Testcase: Detects a file in an absolute path that includes 
	 * only a wildcard in between.
	 * 
	 * @return void
	 */
	public function testcaseUnknownDirectoryInTreeWithStarWildcardInAbsolutePath() {
		$this->setExpectedException('tx_auxo_IOException');		
		$path = t3lib_extMgm::extPath('auxo') . '*/fix';
		$files = $this->resolver->resolve($path . '/' . 'testA.xml');
		$this->assertEquals(count($files), 0);
	}	
		
	/**
	 * Testcase: Detects a file in an relative path that includes a
	 * wildcard at the end of a directory
	 * 
	 * @return void
	 */
	public function testcaseDirectoryWithWildcardSuffixInRelativePath() {
		$files = $this->resolver->resolve('tests/fix*/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . 'tests/fixtures' . '/' . 'testA.xml');
	}		

	/**
	 * Testcase: Detects a file in an relative path that includes a
	 * wildcard at the end of a directory
	 * 
	 * @return void
	 */
	public function testcaseDirectoryWithStarWildcardInRelativePath() {
		$files = $this->resolver->resolve('*/fix*/' . 'testA.xml');
		$this->assertEquals(count($files), 1);
		$this->assertEquals($files[0], t3lib_extMgm::extPath('auxo') . 'tests/fixtures' . '/' . 'testA.xml');
	}		
	
	/**
	 * Testcase: Detects a file in an relative path that includes a
	 * wildcard at the end of a directory
	 * 
	 * @return void
	 */
	public function testcaseUnknownDirectoryWithStarWildcardInRelativePath() {
		$this->setExpectedException('tx_auxo_IOException');
		$files = $this->resolver->resolve('*/blabla*/' . 'testA.xml');		
		$this->assertEquals(count($files), 0);
	}	

	/**
	 * Testcase: Substitutes placeholders in a path or filename
	 * 
	 * @return void
	 */	
	public function testcaseSubstitutePlaceholder() {
		$this->resolver->setPlaceholder('name', 'testing');
		$this->resolver->setPlaceholder('location', 'berlin');
		$this->assertEquals($this->resolver->substitutePrefixesAndPlaceholder('/path/to/{name}/and/{location}/here'), '/path/to/testing/and/berlin/here');
	}
	
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
		unset($this->resolver);
	}
}